#import <dpsclient/wraps.h>	/* for PSxxx functions */
#import <appkit/Control.h>
#import <appkit/TextField.h>
#import <appkit/ScrollView.h>
#import <appkit/tiff.h>		/* NXImageBitmap */
#import <appkit/graphics.h>
#import <appkit/appkit.h>
#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#import "ToyWin.h"
#import "ToyView.h"
#import "TVController.h"


@implementation ToyWin (Drawing)

- drawView:(unsigned char **)map info:(commonInfo *)cinf
{
	ToyView *view;
	id tmp;

	[scView setHorizScrollerRequired: YES];
	[scView setVertScrollerRequired: YES];
	[scView setBorderType: NX_NOBORDER];
	tmp = [[ToyView alloc] setCommText: commentText];
	[tmp setPreference: [controller preference]];
	if ((view = [tmp initDataPlanes:map info:cinf]) == nil) {
		[tmp free];
		return nil;
	}
	if ((tmp = [scView setDocView: view]) != nil)
		[tmp free];
	[scView setCopyOnScroll: YES];
	[thiswindow display];
	[thiswindow makeKeyAndOrderFront: self];
	[commentText setStringValue: cinf->memo];
	NXPing();
	return self;
}


- (int)drawFromFile:(const char *)fileName or:(NXStream *)stream Num:(int)num
{
	id tmp, view;
	commonInfo *cinf;

	[scView setHorizScrollerRequired: YES];
	[scView setVertScrollerRequired: YES];
	[scView setBorderType: NX_NOBORDER];
	tmp = [[ToyView alloc] setCommText: commentText];
	[tmp setPreference: [controller preference]];
	view = stream
		? [tmp initFromStream: stream]
		: [tmp initFromFile: fileName];
	if (view == nil) {
		[tmp free];
		return Err_OPEN;
	}
	cinf = [view commonInfo];
	[self initLocateWindow:fileName
		Width:cinf->width Height:cinf->height Num:num];
	[self makeComment: cinf];
	[commentText setStringValue: cinf->memo];
	if ((tmp = [scView setDocView: view]) != nil)
		[tmp free];
	[scView setCopyOnScroll: YES];
	[thiswindow display];
	[thiswindow makeKeyAndOrderFront: self];
	NXPing();
	return 0;
}

- makeComment:(commonInfo *)cinf
{
	sprintf(cinf->memo, "%d x %d  %dbit%s  %s", cinf->width,
		cinf->height, cinf->bits, ((cinf->bits > 1) ? "s" : ""),
		(cinf->alpha ? "alpha" : ""));
	return self;
}

- (int)drawToyWin:(const char *)fileName Type:(int)type Num:(int)num
{
	return [self drawFromFile: fileName or: NULL Num:(int)num];
}

@end
